package com.ssuu.ssuusnow.project.manage.menu.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.ssuu.ssuusnow.model.result.ResultVo;
import com.ssuu.ssuusnow.project.manage.menu.entity.Menu;
import com.ssuu.ssuusnow.project.manage.menu.service.impl.MenuServiceImpl;
import com.ssuu.ssuusnow.project.manage.menu_role.service.impl.MenuRoleServiceImpl;
import com.ssuu.ssuusnow.project.manage.user.entity.User;
import com.ssuu.ssuusnow.project.manage.user_role.service.impl.UserRoleServiceImpl;
import com.ssuu.ssuusnow.utils.log.LogPrint;
import com.ssuu.ssuusnow.utils.security.SecurityUtils;
import com.ssuu.ssuusnow.utils.tree.TreeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 * 菜单 前端控制器
 * </p>
 *
 * @author sun
 * @since 2023-06-14
 */
@CrossOrigin
@RestController
@RequestMapping("/menu")
@Api(value = "菜单", tags = "菜单")
@ApiSupport(author = "孙")
public class MenuController {

    @Resource
    MenuServiceImpl service;
    @Resource
    UserRoleServiceImpl userRoleService;
    @Resource
    MenuRoleServiceImpl menuRoleService;

    @PostMapping("/add")
    @LogPrint("新增菜单")
    @ApiOperation(value = "新增菜单", notes = "新增菜单")
    @ApiOperationSupport(ignoreParameters = {"id", "createTime", "updateTime", "updateby", "isdel"})
    public ResultVo add(@RequestBody Menu u) {
        return new ResultVo(service.AddTo(u));
    }

    @PutMapping("/update")
    @LogPrint("修改菜单")
    @ApiOperation(value = "修改菜单", notes = "修改菜单")
    @ApiOperationSupport(ignoreParameters = {"createTime", "updateTime", "updateby", "isdel"})
    public ResultVo update(@RequestBody Menu u) {
        return new ResultVo(service.UpdateTo(u));
    }

    @DeleteMapping("/del/{id}")
    @LogPrint("删除菜单")
    @ApiOperation(value = "删除菜单", notes = "删除菜单")
    public ResultVo del(@PathVariable(value = "id") Long id) {
        return new ResultVo(service.DelTo(id));
    }

    @GetMapping("/getlist")
    @LogPrint("查询菜单")
    @ApiOperation(value = "查询菜单", notes = "查询菜单")
    @ApiOperationSupport(ignoreParameters = {"id", "createTime", "updateTime", "updateby", "isdel"})
    public ResultVo get(Menu u) {
        return new ResultVo(service.listTo(u));
    }

    @GetMapping("/gettree")
    @LogPrint("查询树菜单")
    @ApiOperation(value = "查询树菜单", notes = "查询树菜单")
    @ApiOperationSupport(ignoreParameters = {"id", "createTime", "updateTime", "updateby", "isdel"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "1 查询全部  2 根据登录用户查询", dataType = "Integer",paramType = "query"),
    })
    public ResultVo gettree(@Validated @NotEmpty(message = "类型不能为空") Integer type) {
        Menu m = new Menu();
        if(type==2){
            User lou = SecurityUtils.LoginUsers();
            return new ResultVo(TreeUtils.generateTrees(service.menuAllCD(lou,3)));
        }
//        m.setMenuType(1669181114641731586L);
        List<Menu> list = (List<Menu>)service.listTo(m);
        return new ResultVo(TreeUtils.generateTrees(list));
    }
    @GetMapping("/list")
    @LogPrint("查询当前用户权限")
    @ApiOperation(value = "查询当前用户权限", notes = "查询当前用户权限")
    @ApiOperationSupport(ignoreParameters = {"id", "createTime", "updateTime", "updateby", "isdel"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "1 当前用户权限    2 当前用户权限列表   3 查询用户权限菜单", dataType = "Integer",paramType = "query"),
    })
    public ResultVo getperss(@Validated @NotEmpty(message = "类型不能为空") Integer type) { // 1 当前用户权限    2 当前用户权限列表   3 查询用户菜单
        User lou = SecurityUtils.LoginUsers();
        if(type==2){
            return new ResultVo(service.menuAllCD(lou,1));
        }
        if(type==3){
            return new ResultVo(service.menuAllCD(lou,3));
        }
        return new ResultVo(service.menuAll(lou));
    }
}
